package com.candy.databasedocuments.service.document;

import com.candy.databasedocuments.entity.Table;
import com.candy.databasedocuments.service.AbstractDocumentService;
import com.candy.databasedocuments.utils.FreemarkerUtils;
import freemarker.template.Template;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author suYanLong
 * @version 1.0
 * @date 2023/7/7 11:11
 * @Description html文档
 */
@Component
public class HtmlDocumentServiceImpl extends AbstractDocumentService {

    String templateFileName = "htmlTemplate.html";

    @Override
    public String tableToDocument(List<Table> tableList) throws Exception {
        // 获取模板
        Template template = FreemarkerUtils.getTemplate(templateFileName);
        File file = this.createFile("html");
        Map<String, Object> map = new HashMap<>(2);
        map.put("tableInfos", tableList);
        map.put("databaseName", databaseConfig.getDbAttr().getDbName());
        // 根据模板生成文件
        FileWriter fileWriter = new FileWriter(file);
        template.process(map, fileWriter);
        fileWriter.close();
        return file.getAbsolutePath();
    }

}
